/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufg.inf.si.poo.formula1.Race;

import br.ufg.inf.si.poo.formula1.Library.Library;

/**
 * GridRace
 * 
 * Versao 1.0
 * 
 * Data 22/01/2013
 * 
 * @author Igor
 */
public class GridRace {
    //Atributos
    private int qttDrivers;
    private GridPosition[] gridRace;
    
    //Contrutores
    public GridRace(){
        this.qttDrivers = 0;
        gridRace = new GridPosition[26];
    }

    //Metodos Getters and Setters
    /**
     * Retorna o atributo qttDrivers (quantidade de pilotos no GridRace)
     * @return o atributo qttDrivers
     */
    public int getQttDrivers() {
        return qttDrivers;
    }

    /**
     * Retorna o atributo gridRace (vetor com os dados do Grid da corrida)
     * @return o atributo gridRace
     */
    public GridPosition[] getGridRace() {
        return gridRace;
    }

    /**
     * Adiciona, no vetor gridRace, piloto por piloto até que tenha os 26
     * @param gridRace altera o atributo gridRace
     */
    public void setGridRace(GridPosition gridRace) {
        if(this.qttDrivers < 26){
            this.gridRace[this.qttDrivers] = gridRace;
            this.gridRace[this.qttDrivers].setStartingPosition(gridRace.getPosition());
            this.gridRace[this.qttDrivers].setPosition(0);
            this.qttDrivers = this.qttDrivers + 1;
        }
    }

    //Outros Metodos
    /**
     * Reescrita do metodo toString
     * Preenche o retorno com os dados do relatorio do Grid da Corrida
     * @return String com o relatório de dados do Grid da Corrida
     */
    @Override
    public String toString(){
        long lessTime = 0;
        String data = "";
        
        data += "Pos\tNo\tDriver\t\tTeam\t\tLaps\tTime/Retired\tGrid\tPts" + "\n";

        for(int i = 0;i < this.qttDrivers;i++){
            if(this.gridRace[i] != null){
                if(i == 0) {
                    lessTime = this.gridRace[i].timeTotal();
                }
                if(("DNQ".equals(this.gridRace[i].getStatus()))){
                    data += this.gridRace[i].getStatus() + "\t";
                }else if(("Accident".equals(this.gridRace[i].getStatus()))
                        && this.gridRace[i].getQttLapsRace() < (this.gridRace[i].getQttLapsCircuit()*0.9)){
                            data += "Ret" + "\t";
                }else{
                    data += this.gridRace[i].getPosition() + "\t";
                }
                data += this.gridRace[i].getNumberDriver() + "\t";
                if(this.gridRace[i].getDriver() == null) {
                    data += "null\t\t";
                }
                else {
                    data += Library.setLengthString(this.gridRace[i].getDriver().getDriverName(), 15) + "\t" ;
                }
                data += Library.setLengthString(this.gridRace[i].getDriver().getTeam().getShortName(), 15) + "\t";
                if(!"DNQ".equals(this.gridRace[i].getStatus())){
                    data += this.gridRace[i].getQttLapsRace() + "\t";
                    if(this.gridRace[i].getPosition() == 1){
                        data += Library.convertMilliseconds(lessTime, "HORAS") + "\t";
                    }else if(!"Accident".equals(this.gridRace[i].getStatus())){
                        long diferencaTempo = this.gridRace[i].timeTotal()-lessTime;
                        data += "+" + Library.convertMilliseconds(diferencaTempo, "SEGUNDOS") + " secs" + "\t";
                    }else{
                        data += "Accident" + "\t";
                    }
                    data += this.gridRace[i].getStartingPosition() + "\t";
                }
                if(i < 10){
                    data += this.gridRace[i].getPoints() + "\t";
                }
                data += "\n";
            }
        }
        
        return data;
    }

    /**
     * Atualiza alguns dados dos pilotos após a corrida
     */
    public void updateDataDriversGridRace(){
        int points = 0;

        for(int i = 0;i < this.qttDrivers;i++){
            if(i < 10){
                switch(i){
                    case 0: points = 25; break;
                    case 1: points = 18; break;
                    case 2: points = 15; break;
                    case 3: points = 12; break;
                    case 4: points = 10; break;
                    case 5: points = 8; break;
                    case 6: points = 6; break;
                    case 7: points = 4; break;
                    case 8: points = 2; break;
                    case 9: points = 1; break;
                }
                
                /* Se o Driver esta no podium, ou seja, entre os 3 colocados */
                if(i < 3){
                    this.gridRace[i].getDriver().setPodiums(this.gridRace[i].getDriver().getPodiums() + 1);
                }

                this.gridRace[i].setPoints(points);
                this.gridRace[i].getDriver().setPoints(this.gridRace[i].getDriver().getPoints() + points);
            }
            /* Atualiza os dados Highest */
            //Pilotos
            this.gridRace[i].getDriver().setHighestGridPosition(i+1);
            this.gridRace[i].getDriver().setHighestRaceFinish(i+1);
            
            //Equipes
            this.gridRace[i].getDriver().getTeam().setHighestRaceFinish(i+1);
        }
    }
}